package com.define.ssh.action.monitoring;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.define.ssh.action.admin.BaseAdminAction;
import com.define.ssh.common.JsonUtil;
import com.define.ssh.model.MWarnLevel;
import com.define.ssh.model.MWarnScope;
import com.define.ssh.model.MWarnTarget;
import com.define.ssh.model.Terminal;
import com.define.ssh.model.UserInfo;
import com.define.ssh.service.MWarnMemberService;
import com.define.ssh.service.MWarnScopeService;
import com.define.ssh.service.MWarnTargetService;
import com.define.ssh.service.TbLogService;
import com.define.ssh.util.SystemUtil;
import com.define.ssh.util.Pager.Order;

/**
 *  
 * 
 */
@ParentPackage("admin")
public class Config3Action extends BaseAdminAction {
	
	private static final long serialVersionUID = 4L;
	/** 页面传递参数过来*/
	protected int pageSize;
	protected int pageNum;

	@Resource(name = "mwarnTargetServiceImpl")
	private MWarnTargetService mwarnTargetService;
	
	private List<Terminal> terminalList;
	private List<MWarnScope> mwarnScopeList;
	private List<MWarnLevel> mwarnLevelList;
	
	/**
	 * 列表页
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() {
		
		try {
			

	   DetachedCriteria dt = DetachedCriteria.forClass(Terminal.class);	
	   terminalList=mwarnTargetService.findByCriteria(dt);	
		 
		 DetachedCriteria dt1 = DetachedCriteria.forClass(MWarnScope.class);	
		 mwarnScopeList=mwarnTargetService.findByCriteria(dt1);
		 
		 DetachedCriteria dt2 = DetachedCriteria.forClass(MWarnLevel.class);	
		 mwarnLevelList=mwarnTargetService.findByCriteria(dt2);
		
		HashMap<String, Object> searchMap = new HashMap<String, Object>();
		pager.setPageSize(30);
		pager.setOrder(Order.asc);
		pager.setOrderBy("targetid");
		pager.setSearchMap(searchMap);
		pager =mwarnTargetService.findPager(pager);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "config3";
	}

	
	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() {
		
		
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String targetid = super.getRequest().getParameter("targetid");
		String netid = super.getRequest().getParameter("netid");
		String pname = super.getRequest().getParameter("pname");
		String ip = super.getRequest().getParameter("ip");
		String stype = super.getRequest().getParameter("stype");
		String sname = super.getRequest().getParameter("sname");
		String sdetail = super.getRequest().getParameter("sdetail");
		String warnthreshold = super.getRequest().getParameter("warnthreshold");
		String warnlevel = super.getRequest().getParameter("warnlevel");
		
		try {
		
			 DetachedCriteria dt = DetachedCriteria.forClass(MWarnTarget.class);	
			 dt.add(Restrictions.eq("targetid",Integer.valueOf(targetid)));
			 List<MWarnScope> list=mwarnTargetService.findByCriteria(dt);
			if(list!=null&&list.size()>0){
				
				response.getWriter().write("已存在请重新输入!");
				return null;
			}
			Date updatetime = mwarnTargetService.getUpdateFiled("m_warn_target","updatetime");
			MWarnTarget mwl=new MWarnTarget();
			mwl.setTargetid(Integer.valueOf(targetid));
			mwl.setNetid(Integer.valueOf(netid));
			mwl.setPname(pname);
			mwl.setIp(ip);
			mwl.setStype(Integer.valueOf(stype));
			mwl.setSname(sname);
			mwl.setWarnthreshold(Integer.valueOf(warnthreshold));
			mwl.setSdetail(sdetail);
			mwl.setWarnlevel(Integer.valueOf(warnlevel));
			mwl.setUpdatetype(1);////更新标示 1新增  2修改  3删除
			mwl.setUpdatetime(new Date());
			mwl.setTenable(0);
			mwarnTargetService.save(mwl);
			response.getWriter().write("true");
			Long modfiytime = updatetime.getTime() / 1000;
			mwarnTargetService.connectInterfce("m_warn_target", 2004,modfiytime.intValue());
		
		} catch (Exception e) {
			ajax("系统错误!"); 
			e.printStackTrace();
		}	
		
		return null;
	}
	
	/**
	 *详情
	 * @return
	 */
	public String info() {
		
		try {
			String id = super.getRequest().getParameter("id");
			 DetachedCriteria dt = DetachedCriteria.forClass(MWarnTarget.class);	
			 dt.add(Restrictions.eq("id",Long.valueOf(id)));
			 List<MWarnTarget> userlist=mwarnTargetService.findByCriteria(dt);
			 if(userlist!=null&&userlist.size()>0){
				 MWarnTarget mwl= userlist.get(0);
			     return ajax(JsonUtil.beanToJson(mwl));
			 }
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/****
	 * 修改
	 */
	public String update() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String id = super.getRequest().getParameter("id");
		String targetid = super.getRequest().getParameter("targetid");
		String netid = super.getRequest().getParameter("netid");
		String pname = super.getRequest().getParameter("pname");
		String ip = super.getRequest().getParameter("ip");
		String stype = super.getRequest().getParameter("stype");
		String sname = super.getRequest().getParameter("sname");
		String sdetail = super.getRequest().getParameter("sdetail");
		String warnthreshold = super.getRequest().getParameter("warnthreshold");
		String warnlevel = super.getRequest().getParameter("warnlevel");
		
		 DetachedCriteria dt = DetachedCriteria.forClass(MWarnTarget.class);	
		 dt.add(Restrictions.eq("id",Long.valueOf(id)));
		 List<MWarnTarget> list=mwarnTargetService.findByCriteria(dt);
		 if(list!=null&&list.size()>0){
			 Date updatetime = mwarnTargetService.getUpdateFiled("m_warn_target","updatetime");
			 MWarnTarget mwl= list.get(0);
				mwl.setTargetid(Integer.valueOf(targetid));
				mwl.setNetid(Integer.valueOf(netid));
				mwl.setPname(pname);
				mwl.setIp(ip);
				mwl.setStype(Integer.valueOf(stype));
				mwl.setSname(sname);
				mwl.setWarnthreshold(Integer.valueOf(warnthreshold));
				mwl.setSdetail(sdetail);
				mwl.setWarnlevel(Integer.valueOf(warnlevel));
				mwl.setUpdatetype(2);////更新标示 1新增  2修改  3删除
				mwl.setUpdatetime(new Date());
			    mwarnTargetService.update(mwl);
			 response.getWriter().write("true");
			 Long modfiytime = updatetime.getTime() / 1000;
			 mwarnTargetService.connectInterfce("m_warn_target", 2004,modfiytime.intValue());
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/****
	 * 删除
	 */
	public String delete() {
		
		try {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");  
		response.setCharacterEncoding("UTF-8");
		String id = super.getRequest().getParameter("id");
		 DetachedCriteria dt = DetachedCriteria.forClass(MWarnTarget.class);	
		 dt.add(Restrictions.eq("id",Long.valueOf(id)));
		 List<MWarnTarget> userlist=mwarnTargetService.findByCriteria(dt);
		 if(userlist!=null&&userlist.size()>0){
			 Date updatetime = mwarnTargetService.getUpdateFiled("m_warn_target","updatetime");
			 MWarnTarget mwl= userlist.get(0);
			 mwarnTargetService.delete(mwl);
			 response.getWriter().write("true");
			 Long modfiytime = updatetime.getTime() / 1000;
			 mwarnTargetService.connectInterfce("m_warn_target", 2004,modfiytime.intValue());
		 }
		
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	Integer utype;
	/****
	 * 修改状态
	 */
	public String updateState() {

		try {

			String id = super.getRequest().getParameter("id");
			MWarnTarget mwarntarget = mwarnTargetService.get(Long.valueOf(id));
			if (mwarntarget != null && utype != null) {

				if (utype == 0) {
					mwarntarget.setTenable(1);
					mwarnTargetService.update(mwarntarget);
					return ajax("禁用成功!");
				} else if (utype == 1) {
					mwarntarget.setTenable(0);
					mwarnTargetService.update(mwarntarget);
					return ajax("启用成功!");
				}
			}

		} catch (final Exception e) {
			e.printStackTrace();
		}

		return null;

	}
	
	
	public List<Terminal> getTerminalList() {
		return terminalList;
	}

	public void setTerminalList(List<Terminal> terminalList) {
		this.terminalList = terminalList;
	}

	public List<MWarnScope> getMwarnScopeList() {
		return mwarnScopeList;
	}

	public void setMwarnScopeList(List<MWarnScope> mwarnScopeList) {
		this.mwarnScopeList = mwarnScopeList;
	}

	public List<MWarnLevel> getMwarnLevelList() {
		return mwarnLevelList;
	}

	public void setMwarnLevelList(List<MWarnLevel> mwarnLevelList) {
		this.mwarnLevelList = mwarnLevelList;
	}


	public Integer getUtype() {
		return utype;
	}


	public void setUtype(Integer utype) {
		this.utype = utype;
	}
	
	
}